package com.huawei.hmsdemo10086.Activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.POJO.User;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Update_PersonActivity extends AppCompatActivity {

    TextView update_name;
    TextView update_password;
    TextView update_phone;
    Button update_button;
    Intent intent;
    private SharedPreferences sharedPreferences;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_person);


        Button back = (Button)this.findViewById(R.id.button_backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Update_PersonActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userId = sharedPreferences.getString("userId",null);
        update_name = (TextView)findViewById(R.id.update_name);
        update_password = (TextView)findViewById(R.id.update_password);
        update_button = (Button)findViewById(R.id.update_button);
        update_phone = (TextView)findViewById(R.id.update_phone);

        intent = this.getIntent();
        get_person();

        update_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(update_password.length()<=0 || update_name.length()<=0){
                    Toast.makeText(Update_PersonActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
                }else{
                    submit();
                    SharedPreferences sharedPreference=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreference.edit();
                    editor.putString("userName",update_name.getText().toString());
                    editor.commit();
                    AlertDialog.Builder builder = new AlertDialog.Builder(Update_PersonActivity.this);
                    builder.setTitle("更新成功");
                    builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    builder.show();
                }
            }
        });
    }

    public void get_person()
    {
        String url = "http://10.0.2.2:8443/api/findbyid";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + userId+ "\"\n" +
                "}";

        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                String rep = response.body().string();
                System.out.println(rep);
                Type type = new TypeToken<Result<User>>(){}.getType();
                Result<User> result = gson.fromJson(rep,type);
                System.out.println(result.getData().toString());
                update_name.setText(result.getData().getUsername());
                update_password.setText(result.getData().getPassword());
                update_phone.setText(result.getData().getPhone());
            }

        });
    }

    public void submit()
    {
        String url = "http://10.0.2.2:8443/api/updatebyid";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + userId + "\",\n" +
                "  \"username\": \"" + update_name.getText().toString() + "\",\n" +
                "  \"password\": \"" + update_password.getText().toString()  + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }

        });
    }


}